package com.app.util.file;

import java.io.File;

public class FilenameUtil {

	public static final String toGoodFilename(String name, String changeStr) {
		name = name.replaceAll(":", changeStr);
		name = name.replaceAll("\\\\", changeStr);
		name = name.replaceAll("\\/", changeStr);
		name = name.replaceAll("\\?", changeStr);
		name = name.replaceAll("\\*", changeStr);
		name = name.replaceAll("<", changeStr);
		name = name.replaceAll(">", changeStr);
		name = name.replaceAll("\"", changeStr);
		name = name.replaceAll("\\|", changeStr);
		return name;
	}

	public static void main(String[] args) {
		System.out.println(toGoodFilename("a:b\\c/d?e*f<g>h\"i|j", ""));
	}

	public static final String getExtension(File file) {
		return getExtension(file.getName());
	}

	public static final String getExtension(String filename) {
		int i = filename.lastIndexOf('.');
		if (-1 == i)
			return "";
		if (i == 0)
			return "";
		return filename.substring(i + 1);
	}

	public static final String getMainName(File file) {
		return getMainName(file.getName());
	}

	public static final String getMainName(String filename) {
		int i = filename.lastIndexOf('.');
		if (-1 == i)
			return filename;
		if (i == 0)
			return filename;
		if (i == filename.length() - 1)
			return filename;
		return filename.substring(0, i);
	}
 
}
